<?php

namespace App\Model\Api;
use App\Model\BaseModel;
use App\Model\GiftLibrary;
use Illuminate\Support\Facades\Log;

use Illuminate\Database\Eloquent\Model;

class TreasureBoxType extends BaseModel
{
    protected $connection = 'api';
    protected $table = 'treasure_box_type';
    protected $primaryKey = 'type_id';
    private $fields = ['*'];

    public function add($type_id, $data_id, $value, $default_num, $type, $is_used, $new = true) {
        return curl_request(getKkaudioApiUrl() . '/inner/treasure_box/type/upsert', [
            $new === true ? null : 'type_id' => $type_id,
            'data_id' => $data_id,
            'value' => $value,
            'default_num' => $default_num,
            'type' => $type,
            'is_used' => $is_used
        ]);
    }

    public function getList($limit, $page) {
        $query = new self();
        $query = $query->with(['giftLibarary:gift_id,name']);
        return $query->where('type',0)->orderBy('type_id','DESC')->paginate($limit,$this->fields,'page',$page);
    }

    public function giftLibarary() {
        return $this->belongsTo(GiftLibrary::class,'data_id','gift_id')->withDefault([
            'name' => '空'
        ]);
    }
}
